package com.useeinfo.oa.open.admin;

import com.alibaba.fastjson.JSONArray;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.func.lang.StringUtils;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.biz.ApplicationModuleBiz;
import com.useeinfo.oa.modules.base.biz.EnterpriseBiz;
import com.useeinfo.oa.modules.base.entity.ApplicationModule;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.inspection.biz.InspectionOrderBiz;
import com.useeinfo.oa.modules.maintaining.biz.MaintainOrderBiz;
import com.useeinfo.oa.modules.repair.biz.RepairOrderBiz;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import com.useeinfo.oa.modules.task.biz.TaskOrderBiz;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping("/web/admin/getDashBoard")
public class DashBoardAction {

    @Autowired
    private MaintainOrderBiz maintainOrderBiz;
    @Autowired
    private InspectionOrderBiz inspectionOrderBiz;
    @Autowired
    private RepairOrderBiz repairOrderBiz;
    @Autowired
    private EnterpriseBiz enterpriseBiz;
    @Autowired
    private ApplicationModuleBiz applicationModuleBiz;
    @Autowired
    private TaskOrderBiz taskOrderBiz;

    @RequestMapping("/getDashBoard")
    public ModelAndView getDashBoard(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();

        SystemUser systemUser = SessionKeyUtils.getSessionUser(request);
        List<ApplicationModule> applicationModules;
        Enterprise enterprise = new Enterprise();
        //超级管理员可以查看所有
        if (systemUser.getOperationRole().equals("admin")) {
            applicationModules = applicationModuleBiz.findList(new QueryParam("-1", "-1"));
        } else {
            //获取该企业下配置的应用模块
            enterprise = enterpriseBiz.findModel(StringConverters.null2Long(SessionKeyUtils.getSessionEnterprise(request).getId()));
            QueryParam queryParam = new QueryParam();
            queryParam.getSqlMap().put("enterpriseId", enterprise.getId() + "");
            applicationModules=applicationModuleBiz.findList(queryParam);
        }
        List<ApplicationModule>applicationModuleList=new ArrayList<>();
        applicationModules.forEach(x->{
            if(x.getRemark()!=null&&StringUtils.isNotEmpty(x.getRemark()) &&("REPAIR,MAINTAIN,INSPECTION,TASK").contains(x.getRemark())){
                applicationModuleList.add(x);
            }
        });
        int moduleNum = applicationModules.size();
        modelAndView.addObject("moduleNum", moduleNum);//模块数量
        modelAndView.addObject("applicationModuleSet", applicationModuleList);//模块
        long totalNum = 0;
        long repairRecord = 0;
        long maintainRecord = 0;
        long inspectionRecord = 0;
        long taskRecord = 0;
        List<String> unTaking = new ArrayList<>();
        List<String> unComplete = new ArrayList<>();
        List<String> audit = new ArrayList<>();
        List<String> complete = new ArrayList<>();
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("enterpriseId", enterprise.getId() + "");
        queryParam.getSqlMap().put("AUTOMATIC_REPAIR","AUTOMATIC_REPAIR");
        String today = DateTimeUtils.formatDateToString(new Date());
        String remark = "";
        for (ApplicationModule x : applicationModules) {
            if ("REPAIR".equals(x.getRemark())) {
                remark = remark + "," + x.getRemark();
                //维修工单数
                repairRecord = repairOrderBiz.totalRecord(queryParam.getSqlMap());
                unTaking.add(repairOrderBiz.dashBoardInfo(enterprise.getId() + "", "'WAIT_TAKING'", today));
                unComplete.add(repairOrderBiz.dashBoardInfo(enterprise.getId() + "", "'UNFINISHED','REJECTED','WAIT_PRESENCE','WAIT_CONFIRM_FAULT'", today));
                audit.add(repairOrderBiz.dashBoardInfo(enterprise.getId() + "", "'IN_AUDITING'", today));
                complete.add(repairOrderBiz.dashBoardInfo(enterprise.getId() + "", "'COMPLETED'", today));
            } else if ("MAINTAIN".equals(x.getRemark())) {
                remark = remark + "," + x.getRemark();
                //维护保养工单数
                maintainRecord = maintainOrderBiz.totalRecord(queryParam.getSqlMap());
                unTaking.add(maintainOrderBiz.dashBoardInfo(enterprise.getId() + "", "'WAIT_TAKING'", today));
                unComplete.add(maintainOrderBiz.dashBoardInfo(enterprise.getId() + "", "'WAIT_PRESENCE','UNFINISHED'", today));
                audit.add(maintainOrderBiz.dashBoardInfo(enterprise.getId() + "", "'IN_AUDITING'", today));
                complete.add(maintainOrderBiz.dashBoardInfo(enterprise.getId() + "", "'COMPLETED'", today));
            } else if ("INSPECTION".equals(x.getRemark())) {
                //抄表巡检工单数
                remark = remark + "," + x.getRemark();
                unTaking.add(inspectionOrderBiz.dashBoardInfo(enterprise.getId() + "", "'WAIT_TAKING'", today));
                unComplete.add(inspectionOrderBiz.dashBoardInfo(enterprise.getId() + "", "'WAIT_PRESENCE','UNFINISHED'", today));
                audit.add(inspectionOrderBiz.dashBoardInfo(enterprise.getId() + "", "'IN_AUDITING'", today));
                complete.add(inspectionOrderBiz.dashBoardInfo(enterprise.getId() + "", "'COMPLETED'", today));
                inspectionRecord = inspectionOrderBiz.totalRecord(queryParam.getSqlMap());
            }
            else if ("TASK".equals(x.getRemark())) {
                remark = remark + "," + x.getRemark();
                //抄表巡检工单数
                unTaking.add(taskOrderBiz.dashBoardInfo(enterprise.getId() + "", "'WAIT_TAKING'", today));
                unComplete.add(taskOrderBiz.dashBoardInfo(enterprise.getId() + "", "'WAIT_PRESENCE','UNFINISHED'", today));
                audit.add(taskOrderBiz.dashBoardInfo(enterprise.getId() + "", "'IN_AUDITING'", today));
                complete.add(taskOrderBiz.dashBoardInfo(enterprise.getId() + "", "'COMPLETED'", today));
                taskRecord = taskOrderBiz.totalRecord(queryParam.getSqlMap());
            }
        }
        String yesterday = DateTimeUtils.formatDateToString(DateTimeUtils.nextDate(new Date(), "日", -1));
        for (ApplicationModule x : applicationModules) {
            if ("REPAIR".equals(x.getRemark())) {
                //维修工单数
//                repairRecord = repairOrderBiz.totalRecord();
                unTaking.add(repairOrderBiz.dashBoardInfo(enterprise.getId() + "", "'WAIT_TAKING'", yesterday));
                unComplete.add(repairOrderBiz.dashBoardInfo(enterprise.getId() + "", "'UNFINISHED','REJECTED','WAIT_PRESENCE','WAIT_CONFIRM_FAULT'", yesterday));
                audit.add(repairOrderBiz.dashBoardInfo(enterprise.getId() + "", "'IN_AUDITING'", yesterday));
                complete.add(repairOrderBiz.dashBoardInfo(enterprise.getId() + "", "'COMPLETED'", yesterday));
            } else if ("MAINTAIN".equals(x.getRemark())) {
                //维护保养工单数
//                maintainRecord = maintainOrderBiz.totalRecord();
                unTaking.add(maintainOrderBiz.dashBoardInfo(enterprise.getId() + "", "'WAIT_TAKING'", yesterday));
                unComplete.add(maintainOrderBiz.dashBoardInfo(enterprise.getId() + "", "'WAIT_PRESENCE','UNFINISHED'", today));
                audit.add(maintainOrderBiz.dashBoardInfo(enterprise.getId() + "", "'IN_AUDITING'", yesterday));
                complete.add(maintainOrderBiz.dashBoardInfo(enterprise.getId() + "", "'COMPLETED'", yesterday));
            } else if ("INSPECTION".equals(x.getRemark())) {
                //抄表巡检工单数
                unTaking.add(inspectionOrderBiz.dashBoardInfo(enterprise.getId() + "", "'WAIT_TAKING'", yesterday));
                unComplete.add(inspectionOrderBiz.dashBoardInfo(enterprise.getId() + "", "'WAIT_PRESENCE','UNFINISHED'", yesterday));
                audit.add(inspectionOrderBiz.dashBoardInfo(enterprise.getId() + "", "'IN_AUDITING'", yesterday));
                complete.add(inspectionOrderBiz.dashBoardInfo(enterprise.getId() + "", "'COMPLETED'", yesterday));
//                inspectionRecord = inspectionOrderBiz.totalRecord();
            } else if ("TASK".equals(x.getRemark())) {
                //抄表巡检工单数
                unTaking.add(taskOrderBiz.dashBoardInfo(enterprise.getId() + "", "'WAIT_TAKING'", yesterday));
                unComplete.add(taskOrderBiz.dashBoardInfo(enterprise.getId() + "", "'WAIT_PRESENCE','UNFINISHED'", yesterday));
                audit.add(taskOrderBiz.dashBoardInfo(enterprise.getId() + "", "'IN_AUDITING'", yesterday));
                complete.add(taskOrderBiz.dashBoardInfo(enterprise.getId() + "", "'COMPLETED'", yesterday));
//                taskRecord = inspectionOrderBiz.totalRecord();
            }
        }
        //工单总数
        totalNum = repairRecord + maintainRecord + inspectionRecord;
        modelAndView.addObject("repairRecord", repairRecord);
        modelAndView.addObject("maintainRecord", maintainRecord);
        modelAndView.addObject("inspectionRecord", inspectionRecord);
        modelAndView.addObject("totalNum", totalNum);
        modelAndView.addObject("taskRecord", taskRecord);

        modelAndView.addObject("unTakingList", unTaking);
        modelAndView.addObject("unCompleteList", unComplete);
        modelAndView.addObject("auditList", audit);
        modelAndView.addObject("completeList", complete);
        modelAndView.addObject("remark", remark);


        //未接单

        modelAndView.setViewName("pages/home");
        return modelAndView;

    }

    @RequestMapping("/getWelcomeData")
    @ResponseBody
    public JSONObject getWelcomeData(HttpServletRequest request) {
        JSONObject result = new JSONObject();
        SystemUser systemUser = SessionKeyUtils.getSessionUser(request);
        String enterpriseId = null;
        if (!systemUser.getOperationRole().equals("admin")) {
            enterpriseId = String.valueOf(SessionKeyUtils.getSessionEnterprise(request).getId());

        }
        //时间点
        String hourStart = DateTimeUtils.formatDateToString(DateTimeUtils.currentDate());
        String hourEnd = DateTimeUtils.formatDateToString(DateTimeUtils.addHourOfDay(DateTimeUtils.currentDate(), 24));
        String weekStart = DateTimeUtils.formatDateToString(DateTimeUtils.addDayOfMonth(DateTimeUtils.currentDate(), -6));
        String MonthStart = DateTimeUtils.formatDateToString(DateTimeUtils.addDayOfMonth(DateTimeUtils.currentDate(), -29));
        String dayEnd = DateTimeUtils.formatDateToString(DateTimeUtils.addDayOfMonth(DateTimeUtils.currentDate(), 1));

        JSONArray repairTodayModel = repairOrderBiz.getbetween(enterpriseId, hourStart, hourEnd, 1,false);
        result.put("repairTodayModel", repairTodayModel);
        JSONArray repairWeekModel = repairOrderBiz.getbetween(enterpriseId, weekStart, dayEnd, 2,false);
        result.put("repairWeekModel", repairWeekModel);
        JSONArray repairMonthModel = repairOrderBiz.getbetween(enterpriseId, MonthStart, dayEnd, 3,false);
        result.put("repairMonthModel", repairMonthModel);

        JSONArray maintainTodayModel = maintainOrderBiz.getbetween(enterpriseId, hourStart, hourEnd, 1,false);
        result.put("maintainTodayModel", maintainTodayModel);
        JSONArray maintainWeekModel = maintainOrderBiz.getbetween(enterpriseId, weekStart, dayEnd, 2,false);
        result.put("maintainWeekModel", maintainWeekModel);
        JSONArray maintainMonthModel = maintainOrderBiz.getbetween(enterpriseId, MonthStart, dayEnd, 3,false);
        result.put("maintainMonthModel", maintainMonthModel);

        JSONArray inspectionTodayModel = inspectionOrderBiz.getbetween(enterpriseId, hourStart, hourEnd, 1,false);
        result.put("inspectionTodayModel", inspectionTodayModel);
        JSONArray inspectionWeekModel = inspectionOrderBiz.getbetween(enterpriseId, weekStart, dayEnd, 2,false);
        result.put("inspectionWeekModel", inspectionWeekModel);
        JSONArray inspectionMonthModel = inspectionOrderBiz.getbetween(enterpriseId, MonthStart, dayEnd, 3,false);
        result.put("inspectionMonthModel", inspectionMonthModel);


        JSONArray taskTodayModel = taskOrderBiz.getbetween(enterpriseId, hourStart, hourEnd, 1,false);
        result.put("taskTodayModel", taskTodayModel);
        JSONArray taskWeekModel = taskOrderBiz.getbetween(enterpriseId, weekStart, dayEnd, 2,false);
        result.put("taskWeekModel", taskWeekModel);
        JSONArray taskMonthModel = taskOrderBiz.getbetween(enterpriseId, MonthStart, dayEnd, 3,false);
        result.put("taskMonthModel", taskMonthModel);
        return result;
    }


}
